﻿Public Class SelectionTool
    Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Boolean
    Dim ScreenGraphics As Bitmap
    Dim StartSelection As Point
    Dim EndSelection As Point
    Dim X, Y, LocX, LocY As Integer
    Friend Selection As Form
    Friend SelectionBorder As Form
    Dim _MouseDown As Boolean = False
    Dim G As Graphics
    Private Sub SelectionTool_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If My.Computer.Clipboard.ContainsImage = True Then
            ScreenGraphics = New Bitmap(My.Computer.Clipboard.GetImage)
            Me.BackgroundImage = ScreenGraphics
            Dim Thread As New Threading.Thread(AddressOf Loaded)
            Thread.Start()
        Else
            Me.Close()
        End If
    End Sub
    Private Sub Loaded()
        If Me.InvokeRequired = True Then
            Threading.Thread.Sleep(100)
            Me.Invoke(New MethodInvoker(AddressOf Loaded))
        Else
            Me.Size = New Point(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
            Me.Location = New Point(0, 0)
            Me.Opacity = 1.0
        End If
    End Sub
    Private Sub SelectionTool_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        StartSelection = e.Location
        Selection = New Form
        Selection.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Selection.BackColor = Color.Black
        Selection.Opacity = 0.3
        Selection.TopMost = True
        Selection.ShowInTaskbar = False
        Selection.StartPosition = FormStartPosition.Manual
        Selection.Location = e.Location
        SelectionBorder = New Form
        SelectionBorder.Location = e.Location
        SelectionBorder.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        SelectionBorder.StartPosition = FormStartPosition.Manual
        SelectionBorder.BackColor = Color.Black
        SelectionBorder.TransparencyKey = Color.Black
        SelectionBorder.Opacity = 1.0
        Dim Edge1 As New Panel
        Dim Edge2 As New Panel
        Dim Edge3 As New Panel
        Dim Edge4 As New Panel
        Edge1.Dock = DockStyle.Top
        Edge1.BackColor = Color.Red
        Edge1.Size = New Point(100, 1)
        Edge2.Dock = DockStyle.Right
        Edge2.BackColor = Color.Red
        Edge2.Size = New Point(1, 100)
        Edge3.Dock = DockStyle.Left
        Edge3.BackColor = Color.Red
        Edge3.Size = New Point(1, 100)
        Edge4.Dock = DockStyle.Bottom
        Edge4.BackColor = Color.Red
        Edge4.Size = New Point(100, 1)
        SelectionBorder.Controls.Add(Edge1)
        SelectionBorder.Controls.Add(Edge2)
        SelectionBorder.Controls.Add(Edge3)
        SelectionBorder.Controls.Add(Edge4)
        SelectionBorder.TopMost = True
        SelectionBorder.ShowInTaskbar = False
        SelectionBorder.Show()
        Selection.Show()
    End Sub
    Private Sub SelectionTool_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Try
            EndSelection = Cursor.Position
            If EndSelection.X < StartSelection.X Then
                X = StartSelection.X - EndSelection.X
                LocX = StartSelection.X - (StartSelection.X - EndSelection.X)
            Else
                X = EndSelection.X - StartSelection.X
                LocX = StartSelection.X
            End If

            If EndSelection.Y < StartSelection.Y Then
                Y = StartSelection.Y - EndSelection.Y
                LocY = StartSelection.Y - (StartSelection.Y - EndSelection.Y)
            Else
                Y = EndSelection.Y - StartSelection.Y
                LocY = StartSelection.Y
            End If
            Selection.Size = New Point(X, Y)
            Selection.Location = New Point(LocX, LocY)
            Selection.BringToFront()
            SelectionBorder.Size = New Point(X + 2, Y + 2)
            SelectionBorder.Location = New Point(LocX - 1, LocY - 1)
            SelectionBorder.BringToFront()
        Catch ex As Exception
        End Try
    End Sub
    Private Sub SelectionTool_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        SelectionBorder.Close()
        Selection.Close()
        Try
            Dim Path As String = My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Screenshots"
            If My.Computer.FileSystem.DirectoryExists(Path) = False Then MkDir(Path)

            Dim Filename As String = Path & "\Screenshot " & System.IO.Directory.GetFiles(Path).Length - 1 & ".png"

            If My.Computer.FileSystem.FileExists(Filename) Then
                Dim Highest As Integer = 0

                Dim di As New IO.DirectoryInfo(Path)
                Dim diar1 As IO.FileInfo() = di.GetFiles()
                Dim dra As IO.FileInfo

                For Each dra In diar1
                    If IsNumeric(dra.Name.Replace("Screenshot ", Nothing)) Then
                        If dra.Name.Replace("Screenshot ", Nothing) > Highest Then
                            Highest = dra.Name.Replace("Screenshot ", Nothing)
                        End If
                    End If
                Next

                Filename = Path & "\Screenshot " & Highest & ".png"

            End If
            GetFormImage().Save(Filename, System.Drawing.Imaging.ImageFormat.Png)
            FrmUploads.UploadControl1.UploadImage(Filename, True, True, FrmSettings.CmdAuto.Toggled)
            If FrmSettings.CmdShowAnimation.Toggled = True Then Dim Prev As New ImagePreivew(Filename, "Mouse")
        Catch ex As Exception
        End Try

        Me.Close()
    End Sub
    Private Function GetFormImage() As Bitmap
        ' Get this form's Graphics object.
        Dim me_gr As Graphics = Me.CreateGraphics
        me_gr.InterpolationMode = Drawing2D.InterpolationMode.High
        ' Make a Bitmap to hold the image.
        Dim bm As New Bitmap(X, Y, me_gr)
        Dim bm_gr As Graphics = Graphics.FromImage(bm)
        Dim bm_hdc As IntPtr = bm_gr.GetHdc

        ' Get the form's hDC. We must do this after 
        ' creating the new Bitmap, which uses me_gr.
        Dim me_hdc As IntPtr = me_gr.GetHdc

        ' BitBlt the form's image onto the Bitmap.
        BitBlt(bm_hdc, 0, 0, X, Y, me_hdc, LocX, LocY, &HCC0020)
        me_gr.ReleaseHdc(me_hdc)
        bm_gr.ReleaseHdc(bm_hdc)

        Return bm
    End Function
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Opacity = 0.0
    End Sub
End Class